package com.github.ecbp.user.service.controller.front;

import com.github.ecbp.common.api.CommonResult;
import com.github.ecbp.common.data.builder.CommonPageBuilder;
import com.github.ecbp.common.data.controller.BaseController;
import com.github.ecbp.common.req.BaseIdDTO;
import com.github.ecbp.user.service.config.AdminSecurityConfig;
import com.github.ecbp.user.service.dto.PermissionDto;
import com.github.ecbp.user.service.dto.PermissionQueryDto;
import com.github.ecbp.user.service.response.PermissionRes;
import com.github.ecbp.user.service.service.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;

import static com.github.ecbp.common.constant.CacheKeyConstant.PROJECT_NAME;

/**
 * <p>
 * 后台权限表 前端控制器
 * </p>
 *
 * @author zj2626
 * @since 2020-03-18
 */
@Api(tags = "后台权限管理")
@RequestMapping("/api/" + PROJECT_NAME + "/permission")
@RestController
public class PermissionController extends BaseController {
    @Autowired
    private PermissionService service;

    @Autowired
    private AdminSecurityConfig adminSecurityConfig;

    @ApiOperation(value = "刷新缓存中的权限信息")
    @GetMapping(value = "/reload/source")
    public CommonResult initSecurityService() {
        reloadPermission(null);
        return CommonResult.succeed();
    }

    @ApiOperation(value = "添加后台权限")
    @PostMapping(value = "/create")
    public CommonResult create(@Validated @RequestBody PermissionDto param) {
        service.create(param);
        reloadPermission(null);
        return CommonResult.succeed();
    }

    @ApiOperation(value = "修改后台权限")
    @PostMapping(value = "/update")
    public CommonResult update(@Validated(BaseIdDTO.NotNullId.class) @RequestBody PermissionDto param) {
        service.updateById(param);
        reloadPermission(param.getId());
        return CommonResult.succeed();
    }

    @ApiOperation(value = "根据ID获取权限详情", response = PermissionRes.class)
    @PostMapping(value = "/info")
    public CommonResult getItem(@Validated(BaseIdDTO.NotNullId.class) @RequestBody BaseIdDTO param) {
        return CommonResult.succeed(service.getById(param.getId()));
    }

    @ApiOperation(value = "根据ID删除后台权限")
    @PostMapping(value = "/delete")
    public CommonResult delete(@Validated(BaseIdDTO.NotNullId.class) @RequestBody BaseIdDTO param) {
        service.removeByIdBase(param.getId());
        reloadPermission(param.getId());
        return CommonResult.succeed();
    }

    /**
     * 重新初始化缓存中的所有权限信息
     */
    private void reloadPermission(Long id) {
        adminSecurityConfig.reloadTotalPermission();
        if (null != id) {
            service.reloadUserPermission(Collections.singleton(id));
        }
    }

    @ApiOperation(value = "分页列表-后台权限", response = PermissionRes.class)
    @PostMapping(value = "/selectPage")
    public CommonResult selectPage(@Validated @RequestBody PermissionQueryDto param) {
        return CommonResult.succeed(CommonPageBuilder.build(service.page(param, PermissionRes.class)));
    }
}
